package com.nebula.api.modules.member.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nebula.api.modules.member.entity.LoginQo;
import com.nebula.commons.modules.log.LogModules;

import com.nebula.api.modules.member.entity.CmfMember;
import com.nebula.api.modules.member.service.ICmfMemberService;

import link.ahsj.core.annotations.AddGroup;
import link.ahsj.core.annotations.SameUrlData;
import link.ahsj.core.annotations.SysLog;
import link.ahsj.core.annotations.UpdateGroup;
import link.ahsj.core.entitys.ApiModel;
import link.ahsj.core.utils.base.AppAssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ChanlunLee
 * @since 2022-10-10
 */
@RestController
@RequestMapping("cms")
public class CmfMemberController {

    @Autowired
    private ICmfMemberService entityService;

    private static final String LogModule = "表说明";

    @RequestMapping("list")
    public ResponseEntity<ApiModel> page(Page page) {
        return ResponseEntity.ok(ApiModel.data(entityService.page(page)));
    }

    @SameUrlData
    @PostMapping("save")
    @SysLog(value = LogModules.SAVE, module = LogModule)
    public ResponseEntity<ApiModel> save(@Validated({AddGroup.class}) @RequestBody CmfMember entity) {
        entityService.save(entity);
        return ResponseEntity.ok(ApiModel.ok());
    }

    @SameUrlData
    @SysLog(value = LogModules.UPDATE, module = LogModule)
    @PutMapping("update")
    public ResponseEntity<ApiModel> update(@Validated({UpdateGroup.class}) @RequestBody CmfMember entity) {
        entityService.updateById(entity);
        return ResponseEntity.ok(ApiModel.ok());
    }

    @SysLog(value = LogModules.DELETE, module = LogModule)
    @DeleteMapping("delete/{id}")
    public ResponseEntity<ApiModel> delete(@PathVariable("id") String id) {
        entityService.removeById(id);
        return ResponseEntity.ok(ApiModel.ok());
    }

    //登录
//    /phoneRegin
//    @PostMapping("/phoneRegin"):处理post请求方式，用来响应/phoneRegin方法
//    ResponseEntity:标识整个http响应，包含状态码，头部信息，内容
    @PostMapping("/phoneRegin")
    public ResponseEntity<ApiModel> phoneRegin(@RequestBody LoginQo loginQo) {

        //判断账号密码不能为空
        AppAssertUtil.isBlank(loginQo.getUsername(), "账号密码错误");
        AppAssertUtil.isBlank(loginQo.getPassword(), "账号密码错误");

        //在controller层调用service层方法
        String token = entityService.login(loginQo);
        System.out.println(token);

        //最终要返回token
        return ResponseEntity.ok(ApiModel.data(token));
    }
}
